package main.java.extract;

public class Employee {

    static final int ENGINEER = 0;
    static final int SALESMAN = 1;
    static final int MANAGER = 2;

    /**
     * 根据类型码创建对象
     * @param type
     * @return
     */
    static Employee create(int type) {
        switch (type) {
            case ENGINEER:
                return new Engineer();
            case SALESMAN:
                return new Salesman();
            case MANAGER:
                return new Manager();
            default:
                throw new IllegalArgumentException("Incorrect type code value");
        }
    }

    /**
     * 根据字符串创建子类对象
     * @param type
     * @return
     */
    static Employee createFactory(int type) {
        switch (type) {
            case ENGINEER:
                return create("engineer");
            case SALESMAN:
                return create("salesman");
            case MANAGER:
                return create("manager");
            default:
                throw new IllegalArgumentException("Incorrect type code value");
        }
    }

    static Employee create(String name) {
        try {
            return (Employee) Class.forName(name).newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to instance" + name);
        }
    }
}
